package org.leumasjaffe.initiativeTracker;
import static org.leumasjaffe.initiativeTracker.operator.InitiativeController.tracker;

import org.leumasjaffe.initiativeTracker.distributed.controller.ClientProtocolFactory;
import org.leumasjaffe.initiativeTracker.event.InitiativeTrackerAction;
import org.leumasjaffe.initiativeTracker.view.InitiativeFrame;
import org.leumasjaffe.util.io.IPAddress;

import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.ServerAccess;

/** Launch command-line Client to show minimal access needs. */
public class ClientLauncher {

	// If requested by ClientLauncher (pass in '-server' as argument).
	public static final String serverHost = "68.187.210.33";
	
	/**
	 * Note that to simplify the coding of this command-client, it declares that it can throw an Exception,
	 * which is typically the failed connection to a server.
	 */
	@SuppressWarnings("null")
	public static void main(String[] args) throws Exception {

		// FIRST thing to do is register the protocol being used. There will be a single class protocol
		// that will be defined and which everyone will use. For now, demonstrate with skeleton protocol.
		if (!Message.configure("initiative.xsd")) {
			System.exit(0);
		}

		// select dedicated server with '-server' options
		String host = "localhost";
		boolean isDM = false;
		boolean uniAct = false;
		String id = "";

		int l = args.length;
		if (l > 0) {
			for (int i = 0; i < l; i++) {
				String read = args[i];
				if (read.startsWith("-d")) {
					isDM = Boolean.parseBoolean(read.substring(2));
				}
				else if (read.equals("-u")) {
					uniAct = Boolean.parseBoolean(read.substring(2));
				}
				else if (read.equals("-server")) {
					if (i < l-1 && IPAddress.ipv4(args[i+1]))
						host = args[++i];
					else
						host = serverHost;
				}
				else if (i == l - 1)
					id = args[i];
			}
		}

		Constants.client = true;
		Constants.isDM = isDM;
		Constants.uniAct = uniAct;
		Constants.id = id;
		
//		CharacterPanel.updateBounds();
//		InitiativeFrame.updateBounds();
//		ControlPanel.updateBounds();
		
		InitiativeFrame.load(tracker(id));
		
		InitiativeTrackerAction.init();
		
		// try to connect to the server. Once connected, messages are going to be processed by 
		// SampleClientMessageHandler. For now we just continue
		// on with the initialization because
		// no message is actually sent by the connect method.
		ServerAccess sa = new ServerAccess(new ClientProtocolFactory(), host);
		if (!sa.connect()) {
			System.out.println("Unable to connect to server (" + host + "). Exiting.");
			System.exit(0);
		}
		System.out.println("Connected to " + host + "\n");

		// subtle nuance because app is being referred to inside the anonymous class,
		// we have to tell the java compiler that this 'app' variable is not going
		// to be changed once created.

		// send an introductory connect request
		String xmlString = Message.requestHeader() + "<connectRequest game='" + id + "'/></request>";
		Message m = new Message (xmlString);
		sa.sendRequest(m);
	} 
}
